package com.abxh.jetpack.base

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProvider
import com.abxh.jetpack.IViewModel
import java.lang.reflect.ParameterizedType

/**
 * Created by dab on 2021/8/10 14:54
 */
open class BaseViewModelActivity<VM : IViewModel> : AppCompatActivity() {
    lateinit var viewModel: VM
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
/*利用反射获取类实例*/
        val persistentClass =
            (javaClass.genericSuperclass as ParameterizedType).actualTypeArguments[0] as Class<VM>
        viewModel = ViewModelProvider(
            this,
            ViewModelProvider.AndroidViewModelFactory.getInstance(application)
        ).get(persistentClass)
    }


}
